package chat.client.view;

import chat.client.entity.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ConcurrentHashMap;

public class LoginRegisterUI extends JFrame {
    private CardLayout cardLayout;
    private JPanel cardPanel;

    public static ConcurrentHashMap<String, User> userConcurrentHashMap = new ConcurrentHashMap<>();

    public LoginRegisterUI() {
        setTitle("登陆注册界面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null); // 居中显示

        // 创建卡片布局和面板
        cardLayout = new CardLayout();
        cardPanel = new JPanel(cardLayout);

        // 创建登陆界面和注册界面
        LoginPanel loginPanel = new LoginPanel();
        RegisterPanel registerPanel = new RegisterPanel();

        // 添加登陆界面和注册界面到卡片面板
        cardPanel.add(loginPanel, "login");
        cardPanel.add(registerPanel, "register");

        // 添加卡片面板到窗口
        add(cardPanel);

        // 设置默认显示登陆界面
        cardLayout.show(cardPanel, "login");
    }

    // 登陆界面
    class LoginPanel extends JPanel {
        private JTextField usernameField;
        private JPasswordField passwordField;

        public LoginPanel() {
            setLayout(new GridBagLayout());

            JLabel usernameLabel = new JLabel("用户名:");
            usernameField = new JTextField(15);
            JLabel passwordLabel = new JLabel("密码:");
            passwordField = new JPasswordField(15);
            JButton loginButton = new JButton("登陆");
            JButton regionButton = new JButton("去注册");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(10, 10, 5, 10);
            gbc.anchor = GridBagConstraints.WEST;
            add(usernameLabel, gbc);

            gbc.gridy = 1;
            add(passwordLabel, gbc);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            add(usernameField, gbc);

            gbc.gridy = 1;
            add(passwordField, gbc);

            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.gridwidth = 2;
            gbc.insets = new Insets(10, 10, 5, 10);
            gbc.anchor = GridBagConstraints.CENTER;
            add(loginButton, gbc);
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.gridwidth = 2;
            gbc.insets = new Insets(10, 10, 5, 10);
            gbc.anchor = GridBagConstraints.CENTER;
            add(regionButton, gbc);
            loginButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String username = usernameField.getText();
                    String password = new String(passwordField.getPassword());
                    User user = userConcurrentHashMap.get(username);
                    if (user == null) {
                        JOptionPane.showMessageDialog(LoginPanel.this, "用户不存在");
                        return;
                    }
                    if (!user.getPassword().equals(password)) {
                        JOptionPane.showMessageDialog(LoginPanel.this, "密码错误");
                        return;
                    }
                    // 在此处执行登陆逻辑
                    JOptionPane.showMessageDialog(LoginPanel.this, "登录成功，点击确定跳转");

                }
            });
            regionButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    cardLayout.show(cardPanel, "register");
                }
            });
        }
    }

    // 注册界面
    class RegisterPanel extends JPanel {
        private JTextField usernameField;
        private JPasswordField passwordField;
        private JPasswordField confirmPasswordField;

        public RegisterPanel() {
            setLayout(new GridBagLayout());

            JLabel usernameLabel = new JLabel("用户名:");
            usernameField = new JTextField(15);
            JLabel passwordLabel = new JLabel("密码:");
            passwordField = new JPasswordField(15);
            JLabel confirmPasswordLabel = new JLabel("确认密码:");
            confirmPasswordField = new JPasswordField(15);
            JButton registerButton = new JButton("注册");
            JButton switchToLoginButton = new JButton("返回登陆");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(10, 10, 5, 10);
            gbc.anchor = GridBagConstraints.WEST;
            add(usernameLabel, gbc);

            gbc.gridy = 1;
            add(passwordLabel, gbc);

            gbc.gridy = 2;
            add(confirmPasswordLabel, gbc);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            add(usernameField, gbc);

            gbc.gridy = 1;
            add(passwordField, gbc);

            gbc.gridy = 2;
            add(confirmPasswordField, gbc);

            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.gridwidth = 2;
            gbc.insets = new Insets(10, 10, 5, 10);
            gbc.anchor = GridBagConstraints.CENTER;
            add(registerButton, gbc);

            gbc.gridy = 4;
            add(switchToLoginButton, gbc);

            registerButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String username = usernameField.getText();
                    String password = new String(passwordField.getPassword());
                    String confirmPassword = new String(confirmPasswordField.getPassword());
                    if (username.length() < 2) {
                        JOptionPane.showMessageDialog(RegisterPanel.this, "用户名不能过短");
                        return;
                    }
                    if (password.length() < 2) {
                        JOptionPane.showMessageDialog(RegisterPanel.this, "密码过短");
                        return;
                    }
                    if (!password.equals(confirmPassword)) {
                        JOptionPane.showMessageDialog(RegisterPanel.this, "两次密码不匹配");
                        return;
                    }
                    if (userConcurrentHashMap.get(username) != null) {
                        JOptionPane.showMessageDialog(RegisterPanel.this, "用户已经存在");
                        return;
                    }
                    User user = new User();
                    user.setUsername(username);
                    user.setPassword(password);
                    userConcurrentHashMap.put(username, user);
                    JOptionPane.showMessageDialog(RegisterPanel.this, "注册成功，点击确定返回登录界面");
                    cardLayout.show(cardPanel, "login");
                    // 在此处执行注册逻辑
                }
            });

            switchToLoginButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    cardLayout.show(cardPanel, "login");
                }
            });
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                LoginRegisterUI loginRegisterUI = new LoginRegisterUI();
                loginRegisterUI.setVisible(true);
            }
        });
    }
}